from tools_algorithm import *


class Solution:
    def minimumOperationsToWriteY(self, grid: List[List[int]]) -> int:
        n = len(grid)
        cnty = [0] * 3
        cnt = [0] * 3
        for i in range(n):
            for j in range(n):
                if ((i == j or i + j == n - 1) and i < n // 2) or (
                    i >= n // 2 and j == n // 2
                ):
                    cnty[grid[i][j]] += 1
                else:
                    cnt[grid[i][j]] += 1

        def f(x, y):
            return n * n - cnt[x] - cnty[y]

        ans = inf
        for i in range(3):
            for j in range(1, 3):
                ans = min(f(i, (i + j) % 3), ans)
        return ans


s = Solution()
print(s.minimumOperationsToWriteY(grid=[[1, 2, 2], [1, 1, 0], [0, 1, 0]]))
